<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\facade\View;
use think\response\Json;

class FeedbackContent extends AdminBase
{
    public function lists(): string
    {
        $formId = $this->request->param('form_id', 0, 'intval');
        if (empty($formId)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $feedbackBusiness = new \app\admin\business\FeedbackForm($this->websiteCode);
        $feedbackForm     = $feedbackBusiness->findFeedbackFormById($formId);
        if (empty($feedbackForm)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        View::assign([
            'formId'              => $formId,
            'feedbackFormName'    => $feedbackForm['name'],
            'feedbackFormListUrl' => url('feedbackForm/lists', ['website_code' => $this->websiteCode])]);

        return View::fetch();
    }

    public function list(): Json
    {
        $formId = $this->request->param('form_id', 0, 'intval');
        if (empty($formId)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $feedbackFormBusiness = new \app\admin\business\FeedbackForm($this->websiteCode);
        $feedbackForm         = $feedbackFormBusiness->findFeedbackFormById($formId);
        if (empty($feedbackForm)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $page                    = $this->request->param('page', 1, 'intval');
        $limit                   = $this->request->param('limit', 10, 'intval');
        $title                   = $this->request->param('title', '', 'trim');
        $status                  = $this->request->param('status', 0, 'intval');
        $feedbackContentBusiness = new \app\admin\business\FeedbackContent($this->websiteCode, $feedbackForm['form_code']);

        $where = [];
        if ($title) {
            $where[] = ['title', 'like', '%' . $title . '%'];
        }
        if ($status) {
            $where[] = ['status', '=', $status];
        }

        $adPositionInfo = $feedbackContentBusiness->getFeedbackContentList($page, $limit, $where);
        return showList(config(self::_CONFIG_STATUS_SUCCESS_CODE), 'success', $adPositionInfo['rows'], $adPositionInfo['count']);
    }

    public function edit(): string
    {
        $formId = $this->request->param('form_id', 0, 'intval');
        if (empty($formId)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $feedbackFormBusiness = new \app\admin\business\FeedbackForm($this->websiteCode);
        $feedbackForm         = $feedbackFormBusiness->findFeedbackFormById($formId);
        if (empty($feedbackForm)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $feedbackContentBusiness = new \app\admin\business\FeedbackContent($this->websiteCode, $feedbackForm['form_code']);
        $feedbackContent         = $feedbackContentBusiness->findFeedbackContentById($id, $formId);
        if (empty($feedbackContent)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }
        View::assign(["feedbackContent" => $feedbackContent, 'formId' => $formId]);
        return View::fetch();
    }

    public function doEdit(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $formId           = $this->request->param('form_id', 0, 'intval');
        $feedbackBusiness = new \app\admin\business\FeedbackForm($this->websiteCode);
        $feedbackForm     = $feedbackBusiness->findFeedbackFormById($formId);
        if (empty($feedbackForm)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $id     = $this->request->param('id', 0, 'intval');
        $status = $this->request->param('status', 0, 'intval');

        $data     = ['status' => $status];
        $validate = (new \app\admin\validate\FeedbackContent())->scene('edit');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $adPositionBusiness = new \app\admin\business\FeedbackContent($this->websiteCode, $feedbackForm['form_code']);
        $res                = $adPositionBusiness->updateFeedbackContent($id, $data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $formId           = $this->request->param('form_id', 0, 'intval');
        $feedbackBusiness = new \app\admin\business\FeedbackForm($this->websiteCode);
        $feedbackForm     = $feedbackBusiness->findFeedbackFormById($formId);
        if (empty($feedbackForm)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $feedbackFormBusiness = new \app\admin\business\FeedbackContent($this->websiteCode, $feedbackForm['form_code']);
        $res                  = $feedbackFormBusiness->deleteFeedbackContent($id, $formId);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function batchDoDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $formId           = $this->request->param('form_id', 0, 'intval');
        $feedbackBusiness = new \app\admin\business\FeedbackForm($this->websiteCode);
        $feedbackForm     = $feedbackBusiness->findFeedbackFormById($formId);
        if (empty($feedbackForm)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $ids = $this->request->param("ids");
        if ($ids == "") {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }
        $ids = explode(",", $ids);
        foreach ($ids as $k => $v) {
            $ids[$k] = intval($v);
        }

        $feedbackFormBusiness = new \app\admin\business\FeedbackContent($this->websiteCode, $feedbackForm['form_code']);
        $res                  = $feedbackFormBusiness->batchDeleteFeedbackContent($ids, $formId);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function batchDoCheck(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $formId           = $this->request->param('form_id', 0, 'intval');
        $feedbackBusiness = new \app\admin\business\FeedbackForm($this->websiteCode);
        $feedbackForm     = $feedbackBusiness->findFeedbackFormById($formId);
        if (empty($feedbackForm)) {
            return showList(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $status = $this->request->param('status', 0, 'intval');
        if (empty($status)) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }
        if (!in_array($status, [2, 3])) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $ids = $this->request->param("ids");
        if ($ids == "") {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }
        $ids = explode(",", $ids);
        foreach ($ids as $k => $v) {
            $ids[$k] = intval($v);
        }

        $feedbackFormBusiness = new \app\admin\business\FeedbackContent($this->websiteCode, $feedbackForm['form_code']);
        $res                  = $feedbackFormBusiness->batchDoCheck($ids, $status);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);

    }
}